<template>
  <div>
    <Modal
      :value="modalState"
      :title="title"
      :loading="loading"
      @on-ok="handleSubmit"
      @on-cancel="cancel"
      width="600">
      <Form
        ref="businessModal"
        :model="row"
        :rules="ruleInline"
        inline
        style="width: 100%"
        :label-width="90">
        <FormItem prop="proname" label="项目名称" style="width: 250px">
          <Input type="text" v-model="row.proname" placeholder="请输入项目名称" />
        </FormItem>
        <FormItem prop="trainee" label="培训人" style="width: 250px">
          <Input type="text" v-model="row.trainee" placeholder="请输入培训人姓名" />
        </FormItem>
        <FormItem prop="traintime" label="培训时间" style="width: 250px">
          <DatePicker v-model="row.traintime" type="date" placeholder="请输入培训时间"></DatePicker>
        </FormItem>
        <FormItem prop="uname" label="市场员工" style="width: 250px">
          <Input type="text" v-model="row.uname" placeholder="请输入市场员工" />
        </FormItem>
        <FormItem prop="puname" label="市场主管" style="width: 250px">
          <Input type="text" v-model="row.puname" placeholder="请输入市场主管" />
        </FormItem>
        <!-- <FormItem prop="ctime" label="创建时间" style="width: 250px">
           <DatePicker v-model="row.ctime" type="date" placeholder="请输入创建时间"></DatePicker>
        </FormItem> -->
        <FormItem prop="data" label="资料对接状态" style="width: 250px">
          <Select v-model="row.data" clearable placeholder="请选择资料对接状态">
            <Option value="0">
              未完成
            </Option>
            <Option value="1">
              完成
            </Option>
          </Select>
        </FormItem>
        <FormItem prop="test" label="测试状态" style="width: 250px">
          <Select v-model="row.test" clearable placeholder="请选择测试状态">
            <Option value="0">
              未完成
            </Option>
            <Option value="1">
              完成
            </Option>
          </Select>
        </FormItem>
        <FormItem prop="duedate" label="交付日" style="width: 250px">
           <DatePicker v-model="row.duedate" type="date" placeholder="请输入交付日"></DatePicker>
        </FormItem>
        <FormItem prop="traindes" label="培训评价" style="width: 100%">
          <Input v-model="row.traindes" type="textarea" placeholder="请输入培训评价" />
        </FormItem>
      </Form>
    </Modal>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
export default {
  name: 'BusinessModal',
  data () {
    return {
      title: '',
      loading: true,
      form: {
        ctime: '',
        data: '',
        test: '',
        trainee: '',
        traintime: '',
        duedate: '',
        traindes: ''
      },
      ruleInline: {
        trainee: [
          { required: true, message: '培训人姓名不能为空', trigger: 'blur' }
        ],
        proname: [
          { required: true, message: '项目名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  updated () {
    if (this.row.id && this.row.id !== '') {
      this.title = '修改培训信息'
    } else {
      this.title = '添加培训记录'
    }
  },
  computed: {
    ...mapState({
      row: state => state.business.row,
      modalState: state => state.modal.modalState,
      project: state => state.operate.project
    })
  },
  methods: {
    ...mapActions(['updateModalState']),
    changeLoading () {
      this.loading = false
      this.$nextTick(() => {
        this.loading = true
      })
    },
    handleSubmit () {
      this.$refs.businessModal.validate((valid) => {
        if (!valid) {
          return this.changeLoading()
        }
        this.$emit('business-valid', {
          id: this.row.id,
          proname: this.row.proname ? this.row.proname : '',
          uname: this.row.uname ? this.row.uname : '',
          puname: this.row.puname ? this.row.puname : '',
          // ctime: this.row.ctime ? this.$moment(this.row.ctime).format('YYYY-MM-DD HH:mm:ss') : '',
          data: this.row.data ? parseInt(this.row.data) : '',
          test: this.row.test ? parseInt(this.row.test) : '',
          trainee: this.row.trainee,
          traintime: this.row.traintime ? this.$moment(this.row.traintime).format('YYYY-MM-DD HH:mm:ss') : '',
          duedate: this.row.duedate ? this.$moment(this.row.duedate).format('YYYY-MM-DD HH:mm:ss') : '',
          traindes: this.row.traindes
        })
        this.$refs['businessModal'].resetFields()
      })
    },
    cancel () {
      this.updateModalState(false)
      this.$refs['businessModal'].resetFields()
    }
  }
}
</script>
